package com.dup.jxlh.admin.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dup.jxlh.admin.entities.auth.AuthInfo;
import com.dup.jxlh.admin.entities.handler.LevelTypeHandler;
import com.dup.jxlh.admin.entities.model.Admin;
import com.dup.jxlh.admin.entities.vo.AdminVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.One;

import java.math.BigInteger;
import java.util.ArrayList;

@Mapper
public interface AdminMapper extends BaseMapper<Admin> {
    @Select("select admin.* from admin ${ew.customSqlSegment}")
    @Results({
            @Result(column = "level", property = "level"),
            @Result(column = "level", property = "levelName", typeHandler = LevelTypeHandler.class),

            @Result(column = "department_id", property = "departmentId"),
            @Result(column = "department_id", property = "department",
                    one = @One(select = "com.dup.jxlh.admin.mapper.DepartmentMapper.selectById")),

            @Result(column = "role_id", property = "roleId"),
            @Result(column = "role_id", property = "role",
                    one = @One(select = "com.dup.jxlh.admin.mapper.RoleMapper.selectById"))
    })
    ArrayList<AdminVO> getAdminList(Page page, @Param(Constants.WRAPPER) Wrapper<Admin> wrapper);

    @Select("select * from admin where id = #{id}")
    @Results({
            @Result(column = "level", property = "level"),
            @Result(column = "level", property = "levelName", typeHandler = LevelTypeHandler.class),

            @Result(column = "company_id", property = "companyId"),
            @Result(column = "company_id", property = "company",
                    one = @One(select = "com.dup.jxlh.admin.mapper.CompanyMapper.selectById")),

            @Result(column = "department_id", property = "departmentId"),
            @Result(column = "department_id", property = "department",
                    one = @One(select = "com.dup.jxlh.admin.mapper.DepartmentMapper.selectById")),

            @Result(column = "role_id", property = "roleId"),
            @Result(column = "role_id", property = "role",
                    one = @One(select = "com.dup.jxlh.admin.mapper.RoleMapper.selectById"))
    })
    AdminVO getAdminInfo(@Param("id") BigInteger id);

    @Update("update admin set password = #{password} where id = #{id}")
    Boolean updateAdminPassword(@Param("id") BigInteger id, @Param("password") String password);

    @Select("select * from admin where id = #{id}")
    AuthInfo getAuthInfo(@Param("id") BigInteger id);

    @Select("select * from admin where phone = #{phone} and id != #{id}")
    Admin checkAdminByIdAndPhone(@Param("id") BigInteger id, @Param("phone") String phone);
}
